package jojHelicopter;

import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
   
// To play sound using Clip, the process need to be alive.
// Hence, we use a Swing application.
public class Sound 
{
   
	private Clip clip;
	private int timesToRepeat;
	private AudioInputStream audioIn;
	private boolean startedPlaying = false;
	
	
	
    // Constructor
	//public Sound(String soundFileName, int repeatCount) 
	public Sound(URL url, int repeatCount)
	{	           
	  try 
	  {
	     // Open an audio input stream.
	     this.audioIn = AudioSystem.getAudioInputStream(url);

	     // Get a sound clip resource.
	     this.clip = AudioSystem.getClip();
	     
	     // Open audio clip and load samples from the audio input stream.
	     this.clip.open(audioIn);
	     
	     if(repeatCount >= 0)
	     {	 
	    	 this.timesToRepeat = repeatCount;
	     }
	     else
	     {
	    	 this.timesToRepeat = Clip.LOOP_CONTINUOUSLY;
	    	 this.startedPlaying = true;
	     }
	     
	  } catch (UnsupportedAudioFileException e) 
	  {
	        e.printStackTrace();
	  } catch (IOException e) 
	  {
	        e.printStackTrace();
	  } catch (LineUnavailableException e) 
	  {
	        e.printStackTrace();
	  }
   }  
   
   public void play()
   { 
	  if(this.startedPlaying)
	  {
		  this.clip.loop(timesToRepeat);
	  }
	  else
	  {
		  clip.start();
		  this.startedPlaying = true;
	  }
   }
}
